#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "YaoJiaWen"

from django.shortcuts import render,redirect;
from django.http import JsonResponse;
from util.PageVo import *;
import json;
from util.md5_util import *;
from django.db.models import Q;
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage;
from app_account.models import *;
from datetime import datetime;

'''
Account app service
'''

# 新增和修改 user
def insert_update_user(request):
    if request.method == "POST":
        user_dict = json.loads(request.body);
        print(user_dict);
        user = User(
            user_name=user_dict.get("userName"),
            password=get_md5(user_dict.get("password")),
            age=user_dict.get("age"),
            email=user_dict.get("email"),
            create_date=datetime.now().strftime("%Y-%m-%d %H:%M-%S")
        );
        user.save();
        return JsonResponse(Result(200, "Insert success.", user.user_dict()).result());
    elif request.method == "PUT":
        user_dict = json.loads(request.body);
        print(user_dict);
        # 根据id查询数据库对应的user
        user = User.objects.get(id=user_dict.get("id"));
        user.user_name = user_dict.get("userName");
        # user.password = get_md5(user_dict.get("password"));
        user.age = user_dict.get("age");
        user.email = user_dict.get("email");
        user.save();
        return JsonResponse(Result(200, "Update success.", user.user_dict()).result());
    else:
        return JsonResponse(Result(500, "Unsurport request method.").result());

# 根据id查询和删除user
def get_delete_user(request, id):
    if request.method == "GET":
        user = User.objects.get(id=id);
        return JsonResponse(user.user_dict());
    elif request.method == "DELETE":
        User.objects.get(id=id).delete();
        return JsonResponse(Result(200, "Delete success.").result());
    else:
        return JsonResponse(Result(500, "Unsurport request method.").result());

def get_users(request):
    if request.method == "POST":
        search = Search_Vo(json.loads(request.body));
        print(search.__dict__);
        # 使用 keyword 进行模糊匹配
        users = User.objects.filter(Q(user_name__icontains=search.keyword) |
                            Q(email__icontains=search.keyword)).order_by(search.sort);

        paginator = Paginator(users, search.page_size);
        try:
            users = paginator.page(search.current_page);
        except PageNotAnInteger as e:
            search.current_page = 1;
            users = paginator.page(1);
        except EmptyPage as e:
            search.current_page= paginator.num_pages;
            users = paginator.page(search.current_page);

        user_list = list(user.user_dict() for user in users);
        return JsonResponse(Page_Info(
            total=paginator.count,
            pages=paginator.num_pages,
            current_page=search.current_page,
            page_size=search.page_size,
            list=user_list
        ).result());
    else:
        return JsonResponse(Result(500, "Unsurport request method.").result());